package com.lottery.material.controller.admin.circle;

import com.lottery.material.controller.base.BaseController;
import com.lottery.material.enumeration.ResultEnum;
import com.lottery.material.pojo.circle.CircleRuleConfig;
import com.lottery.material.pojo.page.Page;
import com.lottery.material.pojo.page.PageData;
import com.lottery.material.pojocustom.circle.CircleRuleConfigCustom;
import com.lottery.material.service.admin.circle.CircleRuleConfigService;
import com.lottery.material.util.Jurisdiction;
import com.lottery.material.util.PdUtil;
import com.lottery.material.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Description：正式圈员申请规则配置Controller
 * Created by 朱正磊 on 2019-04-13 20:16
 */
@Controller("adminCircleRuleConfigController")
@RequestMapping("/admin/circleRuleConfig")
public class CircleRuleConfigController extends BaseController {

    //菜单地址，用于权限控制
    private String menuUrl = "admin/circleRuleConfig/list";

    private final CircleRuleConfigService circleRuleConfigService;

    @Autowired
    public CircleRuleConfigController(CircleRuleConfigService circleRuleConfigService) {
        this.circleRuleConfigService = circleRuleConfigService;
    }

    /**
     * description：分页列表
     * user 朱正磊
     * time 2019-04-17 15:06
     * @param page 分页对象
     * @param map 响应数据Map
     * @return 返回模型视图
     */
    @RequestMapping("/list")
    public ModelAndView listPage(Page page, Map<String, Object> map) {
        //分页对象设置页面请求参数
        PageData pd = getPageData();
        PdUtil.rePut(pd);
        page.setPd(pd);

        //查询分页列表
        List<CircleRuleConfigCustom> list = circleRuleConfigService.findCustomListPage(page);
        map.put("list", list);
        map.put("pd", pd);
        map.put("QX", Jurisdiction.getHC());

        return new ModelAndView("circle/circleRuleConfig/listPage", map);
    }

    /**
     * description：详情页面
     * user 朱正磊
     * time 2019-04-17 16:20
     * @param id 主键
     * @param map 响应数据Map
     * @return 返回模型视图
     */
    @RequestMapping("/detailPage")
    public ModelAndView detailPage(Integer id, Map<String, Object> map) {
        if(!Jurisdiction.buttonJurisdiction(menuUrl, "cha")) {//判断是否有查看权限
            return null;
        }

        //根据主键查询扩展类
        CircleRuleConfigCustom circleRuleConfig = circleRuleConfigService.findCustomById(id);
        map.put("circleRuleConfig", circleRuleConfig);

        return new ModelAndView("circle/circleRuleConfig/detailPage", map);
    }

    /**
     * description：添加页面
     * user 朱正磊
     * time 2019-04-17 16:26
     * @param map 响应数据Map
     * @return 返回模型视图
     */
    @RequestMapping("/addPage")
    public ModelAndView addPage(Map<String, Object> map) {
        if(!Jurisdiction.buttonJurisdiction(menuUrl, "add")) {//判断是否有添加权限
            return null;
        }

        //添加请求地址
        map.put("actionUrl", "admin/circleRuleConfig/add");

        return new ModelAndView("circle/circleRuleConfig/editPage", map);
    }

    /**
     * description：添加
     * user 朱正磊
     * time 2019-04-17 16:39
     * @param circleRuleConfig 对象
     * @param map 响应数据Map
     * @return 返回模型视图
     */
    @RequestMapping("/add")
    public ModelAndView add(CircleRuleConfig circleRuleConfig, Map<String, Object> map) {
        Integer flag = circleRuleConfigService.add(circleRuleConfig);
        if (Objects.equals(flag, 0)) { //添加失败
            throw new RuntimeException(ResultEnum.STATUS_PERSISTENT_OPERATE_SQL_FAIL.getMessage());
        } else {
            map.put("msg","success");
            return new ModelAndView("save_result", map);
        }
    }

    /**
     * description：修改页面
     * user 朱正磊
     * time 2019-04-17 16:48
     * @param id 主键
     * @param map 响应数据Map
     * @return 返回模型视图
     */
    @RequestMapping("/editPage")
    public ModelAndView editPage(Integer id, Map<String, Object> map) {
        if(!Jurisdiction.buttonJurisdiction(menuUrl, "edit")) {//判断是否有修改权限
            return null;
        }

        //根据主键查询扩展类
        CircleRuleConfigCustom circleRuleConfig = circleRuleConfigService.findCustomById(id);
        map.put("circleRuleConfig", circleRuleConfig);

        //修改请求地址
        map.put("actionUrl", "admin/circleRuleConfig/edit");

        return new ModelAndView("circle/circleRuleConfig/editPage", map);
    }

    /**
     * description：修改
     * user 朱正磊
     * time 2019-04-17 16:58
     * @param circleRuleConfig 对象
     * @param map 响应数据Map
     * @return 返回模型视图
     */
    @RequestMapping("/edit")
    public ModelAndView edit(CircleRuleConfig circleRuleConfig, Map<String, Object> map) {
        Integer flag = circleRuleConfigService.updateById(circleRuleConfig);
        if (Objects.equals(flag, 0)) { //修改失败
            throw new RuntimeException(ResultEnum.STATUS_PERSISTENT_OPERATE_SQL_FAIL.getMessage());
        } else {
            map.put("msg","success");
            return new ModelAndView("save_result", map);
        }
    }

    /**
     * description：验证是否允许新增数据（已经存在1条数据不允许再次新增）
     * user 朱正磊
     * time 2019-05-15 14:38
     * @return 返回响应对象
     */
    @RequestMapping("/validateAdd")
    @ResponseBody
    public ResponseResult validateAdd() {
        List<CircleRuleConfig> circleRuleConfigList = circleRuleConfigService.selectNotBeDeleted();
        Integer size = circleRuleConfigList.size();
        if (Objects.equals(size, 1)) {//之前已经新增了1条数据
            return ResponseResult.buildResult(400, "已经存在1条数据，不允许再次新增！");
        }
        return ResponseResult.buildResult(200);
    }

}
